package com.mwcz.hot.work.controller;

import com.alibaba.fastjson.JSONObject;
import com.mwcz.hot.front.service.OrderService;
import com.mwcz.hot.marketing.service.StoreCouponUserService;
import com.mwcz.hot.payment.service.OrderPayService;
import com.mwcz.hot.store.dao.StoreCartDao;
import com.mwcz.hot.store.model.StoreCart;
import com.mwcz.hot.system.dao.SystemUserLevelDao;
import com.mwcz.hot.user.dao.UserLevelDao;
import com.mwcz.hot.user.model.User;
import com.mwcz.hot.user.service.UserService;
import com.mwcz.hot.work.dao.SiteDao;
import com.mwcz.hot.work.dao.WorkDao;
import com.utils.CrmebUtil;
import com.utils.RestTemplateUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作业管理 前端控制器
 *
 * @author Mr.Bai
 * @since 2021-06-18
 */
@Slf4j
@RestController
@RequestMapping("api/admin/work")
@Api(tags = "作业管理 -- 作业管理")
public class WorkController {

    @Autowired
    private WorkDao workDao;

    @Resource
    private StoreCartDao storeCartDao;

    @Resource
    private SiteDao siteDao;

    @Autowired
    private RestTemplateUtil restTemplateUtil;
    @Autowired
    private OrderService orderService;
    @Autowired
    private StoreCouponUserService storeCouponUserService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderPayService orderPayService;

    @Autowired
    private SystemUserLevelDao systemUserLevelDao;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Map<String, Object> selWork() {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> list = workDao.selWorKList();
        result.put("code", 200);
        result.put("data", list);
        result.put("message", "操作成功");
        return result;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Map<String, Object> saveWork(@RequestBody JSONObject param) {
        Map<String, Object> result = new HashMap<>();
        Integer workId = workDao.saveWork(param);
        Map<String, Object> record = new HashMap<>();
        record.put("id", param.getInteger("car_id"));
        record.put("status", "1");
        Integer carId = workDao.updateCar(record);
        //创建购物车
//        StoreCart storeCart =new StoreCart();
//        storeCart.setCartNum(param.getInteger("supply_num"));
//        Map<String,Object> site =siteDao.selById(param.getInteger("site_id"));
//        storeCart.setUid(Integer.parseInt(site.get("uid").toString()));
//        storeCart.setIsNew(true);
//        storeCart.setProductAttrUnique("8");
//        storeCart.setProductId(1);
//        storeCartDao.insert(storeCart);
//
        result.put("code", 200);
        result.put("data", workId);
        result.put("message", "操作成功");
        return result;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Map<String, Object> updateWork(@RequestBody JSONObject param) {
        Map<String, Object> result = new HashMap<>();
        Integer workId = workDao.updateWork(param);
        result.put("code", 200);
        result.put("data", workId);
        result.put("message", "操作成功");
        return result;
    }

    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    public Map<String, Object> createOrder(@RequestBody JSONObject param, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        //创建购物车
        StoreCart storeCart = new StoreCart();
        storeCart.setCartNum(param.getInteger("supply_num"));
        Map<String, Object> site = siteDao.selById(param.getInteger("site_id"));
        User user = userService.getById(site.get("uid").toString());
        storeCart.setUid(user.getUid());
        storeCart.setIsNew(true);
        storeCart.setProductAttrUnique("8");
        storeCart.setProductId(1);
        storeCart.setType("product");
        storeCartDao.insert(storeCart);
        Map<String, Object> confirm = new HashMap<String, Object>();
        confirm.put("addAgain", false);
        confirm.put("bargain", false);
        confirm.put("cartIds", storeCart.getId());
        confirm.put("combination", false);
        confirm.put("isNew", "true");
        confirm.put("secKill", false);
        confirm.put("uid", Integer.parseInt(site.get("uid").toString()));
        String key = orderService.confirmOrderAdmin(confirm);
        storeCouponUserService.getListByCartIdsAdmin(CrmebUtil.stringToArrayInt(storeCart.getId().toString()), user.getUid());
        Map<String, Object> computed = new HashMap<>();
        computed.put("addressId", 4);
        computed.put("couponId", 0);
        computed.put("payType", "yue");
        computed.put("shippingType", 1);
        computed.put("useIntegral", 0);
        orderService.computedOrderAdmin(computed, key, user.getUid());
        orderPayService.paymentAdmin(orderService.createOrderAdmin(key, user,systemUserLevelDao.selectById(user.getLevel()),site), CrmebUtil.getClientIp(request));
        result.put("code", 200);
        result.put("data", storeCart.getId());
        result.put("message", "操作成功");
        return result;
    }

}
